#include "util.h"

// 软延时，系统调度
void delay_ms(uint32_t millisecond)
{
    vTaskDelay(millisecond * configTICK_RATE_HZ / 1000);
}

// 硬延时，消耗cpu时间
void delay_ms_cpu(uint32_t millisecond)
{
    for(uint32_t i=0; i<millisecond; i++){  
        for(uint32_t j=0; j<1000*30; j++){

        }
    }
}

// 设置时间：2024年1月10日 0点0分0秒
void set_time(struct tm datetime)
{
    time_t second = mktime(&datetime);
    struct timeval val = { .tv_sec = second, .tv_usec = 0 };
    settimeofday(&val, NULL);
}

// 获取当前时间并打印日志
void get_time(struct tm* datetime)
{
    struct tm* temp;
    time_t second;
    time(&second);
    temp = localtime(&second);

    datetime->tm_sec = temp->tm_sec;
    datetime->tm_min = temp->tm_min;
    datetime->tm_hour = temp->tm_hour;
    datetime->tm_mday = temp->tm_mday;
    datetime->tm_mon = temp->tm_mon;
    datetime->tm_year = temp->tm_year;
    datetime->tm_wday = temp->tm_wday;
    datetime->tm_yday = temp->tm_yday;
    datetime->tm_isdst = temp->tm_isdst;
    // ESP_LOGI("get_time", "The current time is: %04d/%02d/%02d %02d:%02d:%02d.", datetime->tm_year+1900, datetime->tm_mon+1, datetime->tm_mday, datetime->tm_hour, datetime->tm_min, datetime->tm_sec);
}

// 自定义日志打印，增加帧头时间戳
void MY_LOGI(char* s)
{
    struct tm datetime;
    get_time(&datetime);

    ESP_LOGI("priority_inheritance", "[%02d:%02d:%02d]%s", datetime.tm_hour, datetime.tm_min, datetime.tm_sec, s);
}